package cn.tedu.straw.portal.service.impl;

import cn.tedu.straw.commons.model.User;
import cn.tedu.straw.commons.model.UserCollection;
import cn.tedu.straw.portal.mapper.UserCollectionMapper;
import cn.tedu.straw.portal.service.IUserCollectionService;
import cn.tedu.straw.portal.service.ex.DeleteException;
import cn.tedu.straw.portal.service.ex.InsertException;
import cn.tedu.straw.portal.service.ex.SelectExcption;
import cn.tedu.straw.commons.vo.UserCollectionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2020-07-30
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements IUserCollectionService {


    @Autowired
    IUserCollectionService userCollectionService;
    @Autowired
    UserCollectionMapper userCollectionMapper;
    @Value("${project.collect-list.page-size}")
    private Integer pageSize;


    @Override
    public PageInfo<UserCollectionVO> getUserCollections(Integer userId, Integer page) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        List<UserCollectionVO> userCollections = userCollectionMapper.findUserCollections(userId);

        return new PageInfo<UserCollectionVO>(userCollections);
    }






    //http://localhost:8088/api/v1/userCollection/post/16
    @Override
    @Transactional
    public UserCollection post(Integer userId, Integer questionId) {
      Integer integer = userCollectionMapper.selectByIds(userId, questionId);
        if (integer!=0){
            throw new SelectExcption("已收藏!");
        }
        UserCollection userCollection = new UserCollection();
        userCollection.setUserId(userId);
        userCollection.setQuestionId(questionId);
        userCollection.setCreatedTime(LocalDateTime.now());
        int rows = userCollectionMapper.insert(userCollection);
        if (rows!=1){
            throw new InsertException("收藏失败！服务器忙，请稍后再次尝试！");
        }
        return userCollection;
    }

    @Override
    public Integer deletes(Integer userId, Integer questionId) {
        Integer rows = userCollectionMapper.deletes(userId, questionId);
        if (rows!=1){
            throw new DeleteException("删除失败！服务器忙，请稍后再次尝试！");
        }
        return rows;
    }

    @Override
    public Integer selectByIds(Integer userId, Integer questionId) {
        Integer integer = userCollectionMapper.selectByIds(userId, questionId);
        if (integer!=0){
            throw new SelectExcption("已收藏!");
        }
        return integer;
    }

    /**
     * 用户的收藏数量
     * @param questionId
     * @return
     */
    public Integer countCollect(Integer questionId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("question_id", questionId);
        Integer integer = userCollectionMapper.selectCount(queryWrapper);
        return integer;
    }

}
